export function classNames(...classes: unknown[]): string {
  return classes.filter(Boolean).join(' ')
}

// 或者直接用 Intl.DateTimeFormat 格式化
export function now(): string {
  return new Intl.DateTimeFormat('zh-CN', {
    timeZone: 'Asia/Shanghai',
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    hour12: false
  }).format(new Date())
}

export function today(): string {
  return new Intl.DateTimeFormat('zh-CN', {
    timeZone: 'Asia/Shanghai',
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  }).format(new Date())
}

export function nowMinute(): string {
  const d = new Intl.DateTimeFormat('zh-CN', {
    timeZone: 'Asia/Shanghai',
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    hour12: false
  }).format(new Date())
  return d
}
// 返回时间戳（毫秒）
export function timestamp(): number {
  return Date.now()
}

// 返回几天后的日期（yyyy-MM-dd）
export function afterDays(days: number): string {
  const d = new Date()
  d.setDate(d.getDate() + days)
  return new Intl.DateTimeFormat('zh-CN', {
    timeZone: 'Asia/Shanghai',
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  }).format(d)
}

// 格式化任意 Date
export function formatDate(date: Date, fmt = 'yyyy-MM-dd HH:mm:ss'): string {
  const pad = (n: number) => n.toString().padStart(2, '0')
  const y = date.getFullYear()
  const m = pad(date.getMonth() + 1)
  const d = pad(date.getDate())
  const h = pad(date.getHours())
  const min = pad(date.getMinutes())
  const s = pad(date.getSeconds())
  return fmt
    .replace('yyyy', `${y}`)
    .replace('MM', m)
    .replace('dd', d)
    .replace('HH', h)
    .replace('mm', min)
    .replace('ss', s)
}

// 时间差（毫秒 -> 天/小时/分钟）
export function diff(from: Date, to: Date) {
  const ms = to.getTime() - from.getTime()
  return {
    milliseconds: ms,
    seconds: Math.floor(ms / 1000),
    minutes: Math.floor(ms / (1000 * 60)),
    hours: Math.floor(ms / (1000 * 60 * 60)),
    days: Math.floor(ms / (1000 * 60 * 60 * 24))
  }
}
